from PyQt5.QtWidgets import QMainWindow, QVBoxLayout, QWidget
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure


class PlotWindow(QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setWindowTitle('仿真结果图表')
        self.setGeometry(100, 100, 800, 600)  # 设置窗口大小

        # 创建一个 widget 和布局来容纳图表
        widget = QWidget(self)
        self.setCentralWidget(widget)
        layout = QVBoxLayout(widget)

        # 创建并添加图表画布
        self.canvas = PlotCanvas(self, width=8, height=6)
        layout.addWidget(self.canvas)

    def plot(self, data):
        self.canvas.plot(data)
        self.show()  # 显示窗口


class PlotCanvas(FigureCanvas):
    def __init__(self, parent=None, width=5, height=4, dpi=100):
        fig = Figure(figsize=(width, height), dpi=dpi)
        self.axes = fig.add_subplot(111)
        super().__init__(fig)
        self.setParent(parent)

    def plot(self, data):
        self.axes.clear()
        self.axes.plot(data)
        self.axes.set_title('仿真结果')
        self.axes.set_xlabel('X 轴')
        self.axes.set_ylabel('Y 轴')
        self.draw()
